﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using SyCODE.Blog.Task;
using SyCODE.Component.DataFactory;
using System.IO;
using System.Xml;
using SyCODE.Component;

namespace SyCODE.Blog.Task
{
    /// <summary>
    /// 文章评论的任务实现
    /// </summary>
    [Serializable]
    public class AlbumCommentTask : TaskBase
    {
        /// <summary>
        /// 相册评论任务的反序列化构造方法
        /// </summary>
        /// <param name="info">反序列化所需的数据</param>
        /// <param name="context">反序列化源的上下文</param>
        protected AlbumCommentTask(SerializationInfo info, StreamingContext context)
        {
            Params = (NameValueCollection)info.GetValue("params", typeof(NameValueCollection));
        }

        /// <summary>
        /// 相册评论的任务实现的构造函数
        /// </summary>
        /// <param name="_params">评论任务所需要的键值对数据</param>
        public AlbumCommentTask(NameValueCollection _params)
        {
            Params = _params;
        }

        /// <summary>
        /// 评论任务的执行方法
        /// </summary>
        public override void Execute()
        {
            string u = Params["u"];
            string album = Params["id"];
            string page = Params["page"];
            string path = Path.Combine(AppPath.GetUserPhysicalPath(u), @"album\comment\" + album + @"\");

            string pagePath = Path.Combine(path, page + ".xml");
            //更新评论的静态XML存档文件
            XmlDocument commentDom = new XmlDocument();
            if (File.Exists(pagePath))
            {
                commentDom.Load(pagePath);
            }
            else
            {
                XmlDocument indexDom = new XmlDocument();

                string indexPath = Path.Combine(path, "main.xml");
                indexDom.Load(indexPath);

                XmlElement link = indexDom.CreateElement("a");
                link.SetAttribute("id", page);
                link.SetAttribute("href", page + ".xml");
                link.InnerText = "page " + page;
                indexDom.DocumentElement.SelectSingleNode("body").AppendChild(link);

                indexDom.Save(indexPath);

                commentDom.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><?xml-stylesheet type=\"text/xsl\" href=\"/ui/common/albumcomment.xsl\"?><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>" + u + "-album-" + album + "-comment-page-" + page + "</title></head><body /></html>");
            }

            XmlNode body = commentDom.DocumentElement.SelectSingleNode("body");

            XmlElement comment = commentDom.CreateElement("div");
            if (Params["source"].Trim().Length > 0)
            {
                comment.SetAttribute("id", Params["source"].Trim());
            }
            comment.SetAttribute("name", Params["sign"]);
            comment.SetAttribute("time", DateTime.Now.ToString(StringResource.GetField("datetimeformat")));

            comment.InnerText = Params["content"];
            body.AppendChild(comment);

            commentDom.Save(pagePath);
        }
    }
}